/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils.middleware;

import java.util.HashMap;

import org.testis.orm.classes.T_test_label;
import org.testis.orm.classes.T_test_label_inclusion;
import org.testis.utils.tree.LabelTreeNode;
import org.testis.utils.tree.TristateLabelTreeNode;

/**
 * 
 * @author selcuk
 */
public class TristateLabelTreeDataHandler implements ILabelTreeDataHandler {

    public LabelTreeNode getLabelTree() {
	LabelDataHandler handler = new LabelDataHandler();
	LabelTreeNode root = new TristateLabelTreeNode("root");
	T_test_label[] labels = handler.getLabelsAsArray();
	HashMap labelNodeMap = new HashMap();
	for (int i = 0; i < labels.length; i++) {
	    T_test_label label = labels[i];
	    TristateLabelTreeNode node = new TristateLabelTreeNode(label);
	    labelNodeMap.put(label.getId(), node);
	}
	for (int i = 0; i < labels.length; i++) {
	    T_test_label label = labels[i];
	    TristateLabelTreeNode node = (TristateLabelTreeNode) labelNodeMap
		    .get(label.getId());
	    if (label.t_test_label_inclusion.size() > 0) {
		T_test_label_inclusion[] inclusions = labels[i].t_test_label_inclusion
			.toArray();
		for (int j = 0; j < inclusions.length; j++) {
		    T_test_label_inclusion inclusion = inclusions[j];
		    TristateLabelTreeNode childNode = (TristateLabelTreeNode) labelNodeMap
			    .get(inclusion.getContainee().getId());
		    if (childNode != null) {
			node.addChild(childNode);
		    }
		}
	    }
	    if (label.t_test_label_inclusion1.size() == 0) {
		root.addChild(node);
	    }
	}
	return root;
    }
}
